import { mock } from 'mockjs'
import { builder } from '../util'
import type { MockResponse } from '../type'
import * as UT from '../util'

// 获取日期格式
export const getDateFormat = (type: string) => {
  if (type == 'hour') return 'yyyy-MM-dd hh:mm'
  else if (type == 'week') return 'yyyy-第ww周'
  else if (type == 'quarter') return 'yyyy-第qq季度'
  else if (type == 'month') return 'yyyy-MM'
  else if (type == 'year') return 'yyyy'
  return 'yyyy-MM-dd'
}

// 时间类型   0-日对比，1-月对比，2-年对比，3-区间对比
export const getDateFtm = (type: number) => {
  if (type == 0 || type == 3) return 'yyyy-MM-dd'
  else if (type == 1) return 'yyyy-MM'
  else if (type == 2) return 'yyyy'
  return 'yyyy-MM-dd hh:mm'
}

export default [
  {
    // 随机数据
    url: '/echarts/random',
    method: 'get',
    response: () => {
      const date = new Date()
      const year = date.getFullYear() - 2
      return builder(
        mock({
          'list|3': [
            {
              'year|+1': year,
              'list|12': [
                {
                  'month|+1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
                  date: '@../../year - @month',
                  'value|300-500': 0
                }
              ]
            }
          ]
        }).list
      )
    }
  },
  {
    // 时间范围
    url: '/echarts/data',
    method: 'get',
    response: (e: MockResponse) => {
      let { startTime, endTime, timeInterval } = e.query
      startTime = Number(startTime)
      endTime = Number(endTime)
      const list = UT.getDateList(startTime, endTime, timeInterval)
      const len = list.length

      const ftm = getDateFormat(timeInterval)
      const nowTimeRange = `${UT.formatDate(startTime, ftm)}~${UT.formatDate(endTime, ftm)}`

      return builder(
        mock({
          // 总能耗
          'nowTotalEnergy|1000-100000': 0,
          // 上期能耗
          'oldTotalEnergy|1000-100000': 0,
          // 增长
          'changeRate|-100-100.0-2': 0,
          // 合计时间
          nowTimeRange: nowTimeRange,
          oldTimeRange: nowTimeRange,

          'list|3-5': [
            {
              name: '@csentence(3, 5)',
              [`list|${len}-${len}`]: [
                {
                  'timeStamp|+1': list,
                  'data|500-1000': 0
                }
              ]
            }
          ],

          // 合计
          totalData: {
            name: '合计',
            [`list|${len}-${len}`]: [
              {
                'timeStamp|+1': list,
                'data|1000-10000': 0
              }
            ]
          },

          // 增减量
          [`changeValue|${len}-${len}`]: [
            {
              'timeStamp|+1': list,
              'data|-100-100.0-2': 0
            }
          ],

          // 增减率
          [`changeRatio|${len}-${len}`]: [
            {
              'timeStamp|+1': list,
              'data|-100-100.0-2': 0
            }
          ]
        })
      )
    }
  },
  {
    // 时间节点
    url: '/echarts/times_data',
    method: 'get',
    response: (e: MockResponse) => {
      // queryType 对比日期类型 0-日 1-月 2-年 3-区间
      let { timeStampList, areaIds, nodeNames = '', viewType, queryType } = e.query

      const times = timeStampList.split(',').map(Number)
      if (times.length == 0) {
        return builder([])
      }
      // 节点 id
      const nodes = areaIds.split(',')
      const names = nodeNames.split(',')
      // const nLen = nodes.length
      const ftm = getDateFtm(queryType)
      let list: number[][] = []
      // 多节点视图
      if (viewType == 0) {
        // 日月年对比
        if (queryType < 3) {
          // 单时间
          if (times.length == 1) {
            list = nodes.map(() => {
              return UT.getDateSubList(queryType, times[0])
            })
          } else {
            list = times.map((tsp: any) => {
              tsp = Number(tsp)
              return UT.getDateSubList(queryType, times[0])
            })
          }
        } else {
          // 日期列表为天
          list = nodes.map(() => {
            return UT.getDateList(times[0], times[1], 'day')
          })
        }
      } else {
        // 单节点模型下 单日期-需加上期数据
        // 日月年对比
        if (queryType < 3) {
          if (times.length == 1) {
            for (let i = 0; i < nodes.length; i++) {
              list.push(UT.getDateSubList(queryType, times[0]))
              // 上期
              list.push(UT.getDateSubList(queryType, times[0]))
            }
          } else {
            for (let i = 0; i < nodes.length; i++) {
              for (let j = 0; j < times.length; j++) {
                list.push(UT.getDateSubList(queryType, times[j]))
              }
            }
          }
        } else {
          for (let i = 0; i < nodes.length; i++) {
            list.push(UT.getDateList(times[0], times[1], 'day'))
            // 上期
            list.push(UT.getDateList(times[0], times[1], 'day'))
          }
          console.log(list)
        }
      }

      let len = 0
      const data = list.map((arr, index) => {
        len = arr.length
        let name = mock('@csentence(3, 5)')
        let id = nodes[index]
        // 单节点视图
        if (viewType == 1) {
          // 日月年对比
          let len = 0
          if (queryType < 3) {
            len = times.length == 1 ? 2 : times.length
          } else {
            len = 2
          }
          id = nodes[Math.ceil((index + 1) / len) - 1]
          name = names[Math.ceil((index + 1) / len) - 1]
        } else {
          // 日月年对比
          if (queryType < 3) {
            // 单时间
            if (times.length == 1) {
              name = names[index]
            } else {
              name = names.join('/') + `${UT.formatDate(times[index], ftm)}`
            }
          } else {
            name = names[index]
          }
        }
        return mock({
          name: name,
          id: id,
          [`list|${len}-${len}`]: [
            {
              'timeStamp|+1': arr,
              'data|500-1000': 0
            }
          ]
        })
      })
      return builder({
        list: data,
        // 合计
        totalData: mock({
          name: '合计',
          [`list|${len}-${len}`]: [
            {
              'timeStamp|+1': list[0],
              'data|1000-10000': 0
            }
          ]
        })
      })
    }
  }
]
